﻿using System.Collections;
using System.Linq;
using System.Net;
using System.Xml.Linq;
using PeoplesJukebox.Web.Entities;
using PeoplesJukebox.Web.ServiceClients.Bases;

namespace PeoplesJukebox.Web.ServiceClients
{
    /// <summary>
    /// Client class for media player services
    /// </summary>
    public class PlayerServiceClient : ServiceClientBase
    {
        /// <summary>
        /// Returns the service base address
        /// </summary>
        protected override string BaseAddress
        {
            //get { return string.Format("http://{0}/PeoplesJukebox.Services/PlayerService.svc/", ServiceHost.HostName); }
            get { return "/PeoplesJukebox.Services/PlayerService.svc/"; }
        }

        /// <summary>
        /// Adds event handlers
        /// </summary>
        protected override void AddEventHandlers()
        {
            //do nothing - handlers added/removed when needed 
        }

        /// <summary>
        /// Starts asynchronous call to start playback
        /// </summary>
        public void Start()
        {
            this.StartDownload("start");
        }

        /// <summary>
        /// Starts asynchronous call to queue track
        /// </summary>
        /// <param name="trackId">Track ID</param>
        public void Queue(int trackId)
        {
            this.StartDownload("queue/{0}", trackId);
        }

        /// <summary>
        /// Starts asynchronous call to get currently-playing track
        /// </summary>
        public void GetCurrentTrack()
        {
            base.AddEventHandlers();
            this.StartDownload("nowPlaying");
        }

        /// <summary>
        /// Notifies subscribers when asynchronous download completes
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">EventArgs</param>
        protected override void SearchDownloadComplete(object sender, DownloadStringCompletedEventArgs e)
        {
            base.RemoveEventHandlers();
            base.SearchDownloadComplete(sender, e);
        }

        /// <summary>
        /// Parses XML download results into <see cref="SelectedTrack"/> collection
        /// </summary>
        /// <param name="xmlResult">XML result</param>
        /// <returns>SelectedTrack collection</returns>
        protected override IEnumerable ParseDownloadResults(string xmlResult)
        {
            IEnumerable mediaItems = null;
            if (!string.IsNullOrEmpty(xmlResult))
            {
                XDocument results = XDocument.Parse(xmlResult);
                XName itemName = XName.Get("SelectedTrack", Namespace.MediaItems);
                mediaItems = from mediaItem in results.Descendants()
                             select new SelectedTrack
                             {
                                 Title = (string)mediaItem.Element(XName.Get("Title", Namespace.MediaItems)),
                                 AlbumTitle = (string)mediaItem.Element(XName.Get("AlbumTitle", Namespace.MediaItems)),
                                 ArtistName = (string)mediaItem.Element(XName.Get("ArtistName", Namespace.MediaItems)),
                                 ChosenBy = (string)mediaItem.Element(XName.Get("ChosenBy", Namespace.MediaItems))
                             };
            }
            return mediaItems;
        }
    }
}